fix: Improved OnValueChanged door example#3959
Open
NoelStephensUnity wants to merge 9 commits intodevelop-2.0.0from
Open
fix: Improved OnValueChanged door example#3959NoelStephensUnity wants to merge 9 commits intodevelop-2.0.0from
NoelStephensUnity wants to merge 9 commits intodevelop-2.0.0from
Conversation
…b.com/Unity-Technologies/com.unity.netcode.gameobjects into fix/docs/onvaluechange_example_update
Fixes to the example script
Did another pass over this script with some improvements.
6 tasks
jabbacakes
approved these changes
May 5, 2026
Contributor
jabbacakes
left a comment
There was a problem hiding this comment.
Basic tweaks to the commentary in the code sample, nothing major
| public DoorStates CurrentState => m_State.Value; | ||
|
|
||
| /// <summary> | ||
| /// Invoked while the <see cref="NetworkObject"/> is in the middle of |
Contributor
There was a problem hiding this comment.
Suggested change
| /// Invoked while the <see cref="NetworkObject"/> is in the middle of | |
| /// Invoked while the <see cref="NetworkObject"/> is in the process of |
| public override void OnNetworkSpawn() | ||
| { | ||
| State.OnValueChanged += OnStateChanged; | ||
| // The write authority (server) does not need to know about its |
Contributor
There was a problem hiding this comment.
Suggested change
| // The write authority (server) does not need to know about its | |
| // The write authority (server) doesn't need to know about its |
| { | ||
| State.OnValueChanged += OnStateChanged; | ||
| // The write authority (server) does not need to know about its | ||
| // own changes (for this example) since it is the "single point |
Contributor
There was a problem hiding this comment.
Suggested change
| // own changes (for this example) since it is the "single point | |
| // own changes (for this example) since it's the "single point |
| protected override void OnNetworkPostSpawn() | ||
| { | ||
| // Everyone updates their door state when finished spawning the door | ||
| // in order to assure the door reflects (visually) its current state. |
Contributor
There was a problem hiding this comment.
Suggested change
| // in order to assure the door reflects (visually) its current state. | |
| // to ensure the door reflects (visually) its current state. |
| // in order to assure the door reflects (visually) its current state. | ||
| UpdateFromState(); | ||
|
|
||
| // Begin to start updating this NetworkBehaviour instance once all |
Contributor
There was a problem hiding this comment.
Suggested change
| // Begin to start updating this NetworkBehaviour instance once all | |
| // Begin updating this NetworkBehaviour instance once all |
| } | ||
|
|
||
| /// <summary> | ||
| /// Invoked just before this instance runs through its de-spawn |
Contributor
There was a problem hiding this comment.
Suggested change
| /// Invoked just before this instance runs through its de-spawn | |
| /// Invoked just before this instance runs through its despawn |
| } | ||
|
|
||
| // Stop updating this NetworkBehaviour instance prior to running | ||
| // through the de-spawn process. |
Contributor
There was a problem hiding this comment.
Suggested change
| // through the de-spawn process. | |
| // through the despawn process. |
| /// </summary> | ||
| /// <remarks> | ||
| /// When the previous state equals the current state, we are a client | ||
| /// that is doing its 1st synchronization of this door instance. |
Contributor
There was a problem hiding this comment.
Suggested change
| /// that is doing its 1st synchronization of this door instance. | |
| /// that is doing its first synchronization of this door instance. |
| } | ||
|
|
||
| /// <summary> | ||
| /// Invoke when the state is updated in order to apply the change |
Contributor
There was a problem hiding this comment.
Suggested change
| /// Invoke when the state is updated in order to apply the change | |
| /// Invoke when the state is updated to apply the change |
|
|
||
| public void OnStateChanged(bool previous, bool current) | ||
| /// <summary> | ||
| /// Invoked by either a Host or clients to interact with the door. |
Contributor
There was a problem hiding this comment.
Suggested change
| /// Invoked by either a Host or clients to interact with the door. | |
| /// Invoked by either a host or clients to interact with the door. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Purpose of this PR
Updating the door example to provide a more recommended way of using OnValueChanged and how to update based on changes in state/value. The original door example could lead users to believe that OnValueChanged would be invoked on all clients upon spawn.
Jira ticket
MTT-14954
Changelog
NA
Documentation
Testing & QA (How your changes can be verified during release Playtest)
Functional Testing
Manual testing :
Manual testing doneAutomated tests:
Covered by existing automated testsCovered by new automated testsDoes the change require QA team to:
Review automated tests?Execute manual tests?Provide feedback about the PR?If any boxes above are checked the QA team will be automatically added as a PR reviewer.
Backports
Does not require a backport.